<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
        
      </caption>
      <div>
        <span>
          <input 
          @change="inputChange"
          type="text" placeholder="请输入英雄"
           :value= "inputValue" >
           <input 
            @change="priceChange"
           type="text"
           placeholder="请输入价格"
            :value= "inputPrice">
            <input 
             @change="numChange"
            type="text"
            placeholder="请输入数量"
             :value= "inputNum">
           
          <button @click="addItemToList">添加事项</button></span>
      </div>
      <thead>
          <div >
          <tr>
          <th><input type="checkbox" v-model="isAll" /> <span>全选</span></th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </div>
      </thead>
      <tbody>
        <Min 
      :obj="goodList"
        @isAll=isAll
        @delClick=delFn
        />
      </tbody>
      <tfoot>
        <Footom 
       :arr="goodList"
         />
      </tfoot>
    </table>
  </div>
</template>

<script>
// 1.创建组件
import Min from "./components/Min.vue"
import Footom from "./components/Footom.vue"
import{mapState}from "vuex"
export default {
// 2.注册组件
  components:{
Min,
Footom
  },
  data() {
    return {
     
    };
  },
  // 计算属性
  computed: {
    isAll:{
      // set 重置的意思 把大选框的值赋值给每一项的小选框
      set(val){
        this.goodList.forEach(item=>item.checked=val)
      },
      // get 获取 把小选框的每一项的选中状态返给 全选框
      get(){
        return this.goodList.every(item=>item.checked)
      }
    },
      ...mapState(['goodList','inputValue','inputPrice','inputNum'])

  },
  methods:{
    delFn(index){
      this.goodList.splice(index,1)
    },
    //1. 名称输入框input value
    inputChange(e){
      // console.log(e.target.value);
      this.$store.commit('setInputValue',e.target.value)
    },
    // 价格输入框
    priceChange(e){
      
        this.$store.commit('setPriceValue',e.target.value)
    },
    // 数量输入框
  numChange(e){
  this.$store.commit('setNumValue',e.target.value)
  },
    // 2.向列表中新增
    addItemToList(){
      if(this.inputValue.trim().length <=0 || this.inputPrice.length <=0||this.inputNum.length<=0 ){
        return alert('文本内容不能为空!')
      }
      
      this.$store.commit('addIem')
    }
  }
  

};
</script>

<style scoped>


th{
border:1px solid #ccc;
width: 700px;

}
</style>
